#include "configurationform.h"
#include "databasemanager.h"
#include "downloadmanager.h"
#include <QDebug>

ConfigurationForm::ConfigurationForm(QWidget *parent) :
    QWidget(parent)
{
    folderLine = new FolderConfigureLine();
    urlLine = new UrlConfigureLine();
    priorityLine = new PriorityConfigureLine();
    okButton = new QPushButton("OK", this);

    setLayout(new QVBoxLayout());
    layout()->addWidget(urlLine);
    layout()->addWidget(folderLine);
    layout()->addWidget(priorityLine);
    layout()->addWidget(okButton);

    connect(okButton, SIGNAL(clicked()), this, SLOT(start()));
}

ConfigurationForm::~ConfigurationForm()
{
    delete urlLine;
    delete folderLine;
    delete priorityLine;
    delete okButton;
    delete tableModel;
    delete dataMapper;
}

void ConfigurationForm::emitDbUpdate()
{
    emit dbUpdate();
}

void ConfigurationForm::start()
{
    DownloadManager *download = new DownloadManager();
    connect(download, SIGNAL(progressUpdate()), this, SLOT(emitDbUpdate()));
    int id = DatabaseManager::getDBManager()->insertDownload(urlLine->urlLineEdit->text(), folderLine->folderEdit->text(), 0, priorityLine->prioritySpinBox->value());
    download->startDownload(urlLine->urlLineEdit->text(), folderLine->folderEdit->text(), priorityLine->prioritySpinBox->value(), id);
    emit dbUpdate();
    this->hide();
}
